define(function(require, exports, module){
  'use strict';
  var $ = require('jquery');
  require('bootstrapjs')($);
  // require('icheck')($);
  var fnModal = require('seajs/fn/fnModal');
  var _const = require('seajs/fn/fnConst.js');
  var fnUtils = require('seajs/fn/fnUtils');
  var SimpleMap = require('seajs/plugs/simpleMap');
  var Task = require('seajs/fn/fnTask');
   // common start 
  function onToggleLeftMenu(){
    $('.tg-list > a').on('click', function(){
      var parent = $(this).parent();
      var sub = parent.find('> ul');
      if(sub.is(':visible')) {
        sub.slideUp(200, function(){
          parent.removeClass('nav-active');
          $('.main-content').css({height: ''});
        });
      }else{
        visibleSubMenuClose();
        parent.addClass('nav-active');
        sub.slideDown(200, function(){  
        });
      }
      return false;
    });
  }

  function offToggleLeftMenu(){
    $('.tg-list > a').off('click');
  }

  function visibleSubMenuClose() {
    $('.tg-list').each(function() {
     var t = $(this);
     if(t.hasClass('nav-active')) {
        t.find('> ul').slideUp(200, function(){
          t.removeClass('nav-active');
        });
      }
    });
  }

  var customCheckboxChange = function(key, checked){
     $(key).each(function(index){
        var self = $(this);
        if(self.is(':checked') !== checked){
          if(checked){
            self.prop("checked", 'true').change();
          }else{
            self.removeAttr("checked").change();
          }
        }
     });
  }

  var checkboxEvent = function(ws, checkedMap){

    $('input[tg-tyep="group"]').on('click', function(e){
      var self = $(this), groupId = self.val();
      if(!self.is(':checked')){
        customCheckboxChange('input[tg-group="' + groupId + '"]:not([disabled])', false);
      }else{
        customCheckboxChange('input[tg-group="' + groupId + '"]:not([disabled])', true);
      }
    });


    $('input[tg-tyep="member"]').on('change',function(){
      var self = $(this), groupId = self.attr('tg-group'),
        memberId = self.val(), allChecked = true;
      if(self.is(':checked')){
        checkedMap.put(memberId, ws.slidebar.memberMap.get(memberId));
        //customCheckboxChange('input[value="' + memberId + '"]', true);
        $('input[value="' + memberId + '"]').prop("checked", 'true');
        selectResultAdd(checkedMap.get(memberId));
      }else{
        checkedMap.remove(memberId);
        //customCheckboxChange('input[value="' + memberId + '"]', false);
        $('input[value="' + memberId + '"]').removeAttr('checked');
        selectResultRemove(memberId);
      }

      if(checkedMap.size() === 0){
        $('.temporary-group-buttons > .btn').attr('disabled','disabled');
        $('.temporary-group-lable').html('请勾选需要添加的成员');
      }else{
        //获取临时对讲成员信息 checkedMap.getAll()
        //console.log(checkedMap.getAll());
        $('.temporary-group-buttons > .btn').removeAttr('disabled');
        $('.temporary-group-lable').html('已选择组成员' + checkedMap.size() + '人');
        TemporaryGroup.prototype.selectedMember=checkedMap.map;
        TemporaryGroup.prototype.selectedMemberId=memberId;
      }

      // $('input[tg-group="' + groupId + '"]').each(function(){
      //   if(!$(this).is(':checked')){
      //     allChecked = false;
      //     return false;
      //   }
      // });

      // if(allChecked){
      //   $('#tg_checkbox_group_' + groupId).prop("checked", 'true');
      // }else{
      //   $('#tg_checkbox_group_' + groupId).removeAttr("checked");
      // }
      
    });
  }

  var onSearchResultCheckBoxByUid = function(checkedMap, member){
    var memberId = member.memberId;
    $('#tg_search_rlt_checkbox_member_' + memberId).on('click', function(){
      var self = $(this);
      if(self.is(':checked')){
        checkedMap.put(memberId, member);
        customCheckboxChange('input[value="' + memberId + '"]', true);
        selectResultAdd(checkedMap.get(memberId));
      }else{
        checkedMap.remove(memberId);
        customCheckboxChange('input[value="' + memberId + '"]', false);
        selectResultRemove(memberId);
      }
    });
  }

  var sortMemebers = function(onlines, inothers){
    var merges = onlines.concat(inothers);
    return fnUtils.memberSort(merges);
  }

  var drawSelectResult = function(members){

  }

  var selectResultAdd = function(member){
    if(member){
      if($('li[tg-rlt="' + member.memberId + '"]').length === 0){
        $('#tg_select_result').append('<li name="tg_select_rlt_li" tg-rlt="' 
          + member.memberId +'"><a href="#"><img src="images/online_small.png">' 
          + member.memberName + '<div tg-rlt-remove="' 
          + member.memberId + '"></div></a></li>');
        onSelectResultRemoveByUid(member.memberId);
      }
    }
  }

  var searchResultRemove = function(){
    $('#tg_select_result').html('');
    $('.temporary-group-lable').html('请勾选需要添加的成员');
  }

  var searchResultAdd = function(checkedMap, member, disabledFlag, chkFlag){
    if(member){
      $('#tg_search_rlt').append('<li id="tg_search_rlt_li_' 
        + member.memberId + '"><a href="#"><img src="images/online_small.png">'
        + member.memberName + '<input class="modal-checkbox" id="tg_search_rlt_checkbox_member_' 
        + member.memberId + '" value="' 
        + member.memberId + '" type="checkbox"><label for="tg_search_rlt_checkbox_member_' 
        + member.memberId +'"></label></a></li>');

      onSearchResultCheckBoxByUid(checkedMap, member);
      // searchCheckboxInitByUid(member.memberId);
      if(disabledFlag){
        $('#tg_search_rlt_checkbox_member_' + member.memberId)
        .prop('checked', 'true').attr('disabled', 'disabled');
      }else{
        if(chkFlag){
          $('#tg_search_rlt_checkbox_member_' + member.memberId).prop('checked','true');
        }else{
          $('#tg_search_rlt_checkbox_member_' + member.memberId).removeAttr('checked');
        }
      }
    } 
  }

  var TemporaryGroup = function(){
    this.ws = undefined;
    this.isCreated = undefined;
    this.selectedMap = undefined;
    this.tgMemberMap = undefined;
    this.memberMap = undefined;
    this.memberSortArray = undefined;
    this.timeTaskMap = undefined;
    this.timeCheckJoinMap = undefined;
    this.map = undefined;
    this.groupName = undefined;

    this.joinedMembers = undefined;
    this.init();
  }

  TemporaryGroup.prototype.selectedMember=undefined;

  TemporaryGroup.prototype.init = function(){
    this.isCreated = false;
    this.selectedMap = new SimpleMap();
    this.memberMap = new SimpleMap();
    this.tgMemberMap = new SimpleMap();
    this.timeTaskMap = new SimpleMap();
    this.timeCheckJoinMap = new SimpleMap();
    this.joinedMembers = {};
    //common
    //select
    this.onBtnEvent();
    this.onSelectDialogEvent();

    //Tg
    this.onTGDialogEvent();

  }

  TemporaryGroup.prototype.onBtnEvent = function(){
    var self = this;

    setTimeout(function(){
      self.map=self.ws.slidebar.map;
      console.log(self.ws.slidebar.map);
    },10);
    ////$('a[id="temporary_talking"] > button').on('click', function(){
    //  self.initSelectDialog(self.ws.slidebar.groupMap.map);
    //  self.map=self.ws.slidebar.map;
    //
    //  //console.log(self.map);
    //  self.openSelectDialog();
    //});
    //綁定临时组按钮
    $("#temporary_talking1").on('click', function(){
      if(!self.ws.tasks.currentId){//change111111
        self.initSelectDialog(self.ws.slidebar.groupMap.map);
        self.map=self.ws.slidebar.map;
        self.openSelectDialog();
      }else{
        var status=$('#main_bottom_left_voice_obj').html().slice(0,-1);
        $('#function-alert').html(status+'正在进行,请先关闭'+status+'组在进行其他操作').fadeIn(200,function(){
          setTimeout(function(){
            $('#function-alert').fadeOut(500);
          },1000)
        })
      }

    });
  }

  // select
  TemporaryGroup.prototype.drawMember = function(groupId, members){
    if(groupId && members){
      var memberHtml = [];
      for(var j in members){
        this.memberMap.put(members[j].memberId, members[j]);
        memberHtml.push('<li><a><img src="images/online_small.png">' + members[j].memberName);
        memberHtml.push('<input class="modal-checkbox" name="tg_select_checkbox" id="tg_checkbox_group_' + groupId + '_member_' + members[j].memberId 
          + '" value="' + members[j].memberId + '" type="checkbox" tg-tyep="member" tg-group="' + groupId + '">' 
          + '<label for="tg_checkbox_group_' + groupId + '_member_' + members[j].memberId + '" ></label>'
          + '</a></li>');
      }
      return memberHtml.join('');
    }
  }

  TemporaryGroup.prototype.drawGroup = function(groups){
    if(groups){
      var groupHtml = [];
      for(var i in groups){
        if(groups[i].status){
          groupHtml.push('<li class="menu-list tg-list">');
          groupHtml.push('<a href="#"><img src="images/bottom_curr_group.png">' + groups[i].groupName+ '</a>');
          groupHtml.push('<ul><div id="tg_check_all">全选<input class="modal-checkbox" name="tg_select_checkbox" id="tg_checkbox_group_' + groups[i].groupId 
            + '" value="' + groups[i].groupId + '" type="checkbox" tg-tyep="group">'
            + '<label for="tg_checkbox_group_' + groups[i].groupId + '"></label>'
            + '</div>');
          var members = sortMemebers(groups[i]['members']['online'], groups[i]['members']['inOther']);
          groupHtml.push(this.drawMember(groups[i].groupId, members));
          groupHtml.push('</ul>');

        }
      }
      if(this.memberMap.size()> 0){
        this.memberSortArray = fnUtils.memberSort(this.memberMap.values());
      }
      return groupHtml.join('');
    }
  }

  TemporaryGroup.prototype.setWs = function(ws){
    this.ws = ws;
    this.onFixPTTKey(this.ws);

  }

  TemporaryGroup.prototype.initSelectDialog = function(groups, tgMap){
    var self = this;
    self.clearTGDialog();
    self.selectedMap.clear();
    self.memberMap.clear();
    self.onSelectDismissEvent();
    $('#tg_select_box_ul').html(self.drawGroup(groups));
    onToggleLeftMenu();
    $('#tg_select_result').html('');
    $('#tg_select_title').html('选择临时组成员');
    $('.temporary-group-lable').html('请勾选需要添加的成员');
    $('div[role="dialog"]').modal('hide');
    //checkboxInit();

    checkboxEvent(self.ws, self.selectedMap);
    $('#tg_select_dismiss').off('click').on('click', function(){
      $('#selectTemporaryGroup').modal('hide');
      self.enabledOpenBtn();
    });
    self.onSelectConfirmEvent();
    self.disabledOpenBtn();
    $('.temporary-group-buttons > .btn').attr('disabled','disabled');
    if(tgMap && tgMap.size() > 0){
      for(var i in tgMap.map){
        self.selectedMap.put(i, tgMap.get(i));
        $('input[value="'+ i + '"]').prop('checked', 'true').change();
      }
    }
    $('#tg_search_input').val('');
    $('.temporary-group-select-box').show();
    $('.temporary-group-search-result').hide();
  }

  TemporaryGroup.prototype.openSelectDialog = function(groups){
    $('#selectTemporaryGroup').modal('show');
  }

  TemporaryGroup.prototype.openAddSelectDialog = function(){
    var self = this;
    self.memberMap.clear();
    self.onSelectDismissEvent();

    $('#tg_select_box_ul').html(self.drawGroup(self.ws.slidebar.groupMap.map));
    onToggleLeftMenu();
    checkboxEvent(self.ws, self.selectedMap);
    $('#tg_select_dismiss').off('click').on('click', function(){
      $('#selectTemporaryGroup').modal('hide');
      self.enabledOpenBtn();
    });

    self.onSelectConfirmEvent();
    self.disabledOpenBtn();
    
    if(self.tgMemberMap && self.tgMemberMap.size() > 0){
      for(var i in self.tgMemberMap.map){
        customCheckboxChange('input[value="'+ i + '"]', true);
        $('input[value="'+ i + '"]').attr('disabled','disabled');
      }
    }
    self.selectedMap.clear();
    $('.temporary-group-buttons > .btn').attr('disabled','disabled');
    $('#tg_search_input').val('');
    $('.temporary-group-select-box').show();
    $('.temporary-group-search-result').hide();

    $('#tg_select_title').html('添加临时组成员');
    $('#tg_select_result').html('');
    $('.temporary-group-lable').html('请勾选需要添加的成员');
    $('div[role="dialog"]').modal('hide');
    $('#selectTemporaryGroup').modal('show');

    this.onAddDismissEvent();
    this.onAddConfirmEvent();
  }

  TemporaryGroup.prototype.disabledOpenBtn = function(){
    //$('.temporary_btn').attr('disabled', 'disabled');
    //console.log('禁止使用')
    //$('#temporary_broadcast1').css('color','#ddd');
    //$('#temporary_broadcast1 img').attr('src','images/untemporary_broadcast.png');
    //$('#temporary_talking1').css('color','#ddd');
    //$('#temporary_talking1 img').attr('src','images/untemporary_talking.png');
  }

  TemporaryGroup.prototype.enabledOpenBtn = function(){
    //$('.temporary_btn').removeAttr('disabled');
    //console.log('可以使用')
    //$('#temporary_broadcast1').css('color','#3399ff');
    //$('#temporary_broadcast1 img').attr('src','images/temporary_broadcast.png');
    //$('#temporary_talking1').css('color','#3399ff');
    //$('#temporary_talking1 img').attr('src','images/temporary_talking.png');
  }

  var onSelectResultRemoveByUid = function(memberId){
    $('li[tg-rlt="' + memberId + '"]').on('click', function(){
      selectResultRemove(memberId);
    });
  }

  var selectResultRemove = function(memberId){
    $('li[tg-rlt="' + memberId + '"]').off('click');
    $('li[tg-rlt="' + memberId + '"]').remove();
    customCheckboxChange('input[value="' + memberId + '"]', false);
  }

  TemporaryGroup.prototype.onSelectDialogEvent = function(){
    var self = this;
    $('#tg_search_input').off('input on').on('input on', function(){
      if($(this).val()){
        $('.temporary-group-select-box').hide();
        $('.temporary-group-search-result').show();
        $('#tg_search_rlt').html('');
        for(var i in self.memberSortArray){
          if(self.memberSortArray[i].memberName.indexOf($(this).val()) >= 0 ||
            self.memberSortArray[i].py.indexOf($(this).val().toUpperCase()) >= 0){
            searchResultAdd(self.selectedMap, self.memberSortArray[i], 
              self.tgMemberMap.get(self.memberSortArray[i].memberId) === undefined ? false: true,
              self.selectedMap.get(self.memberSortArray[i].memberId) === undefined ? false: true);
          }
        }
      }else{
        $('.temporary-group-search-result').hide();
        $('.temporary-group-select-box').show();
      }
    });
    
    $('.temporary-group-buttons >.temporary-group-clear').off('click').on('click', function(){
      $('input[name="tg_select_checkbox"]:not([disabled])').attr("checked", false);
      $('input[id*="tg_search_rlt_checkbox_member"]:not([disabled])').attr("checked", false);
      $('#tg_select_result').html('');
      $('.temporary-group-buttons > .btn').attr('disabled','disabled');
      $('.temporary-group-lable').html('请勾选需要添加的成员');
      self.selectedMap.clear();
    });

    self.onSelectConfirmEvent();
    
  }

  // X按钮
  TemporaryGroup.prototype.onSelectDismissEvent = function(){
    $('#tg_select_dismiss').off('click').on('click', function(){
       $('#selectTemporaryGroup').modal('hide');
    });
  }
  TemporaryGroup.prototype.onAddDismissEvent = function(){
    var self = this;
    $('#tg_select_dismiss').off('click').on('click', function(){
      self.openTGDialog();
    });
  }
  //解散临时对讲 回到当前页面
  function overTalkSite(map){
    AMap.plugin(['AMap.Geolocation'],function(){
      var geolocation=new AMap.Geolocation({
        enableHighAccuracy:true,
        covert:true,
        showButton:false,
        panToLocation:true,
        showCircle:false,
        showMarker:true,
        timeout:60000
      })
      geolocation.getCurrentPosition();//获取当前位置
      map.addControl(geolocation);
      map.setZoom(15);
    })

  }
  // 确定 - 创建临时对讲按钮
  function confirmSite(map,path){

    var polygon = new AMap.Polygon({
      map: map,
      path: path,
      strokeOpacity:0,
      fillOpacity:0
    });
    //var name=$('#tg_select_result li').html();
    //console.log(name);
    map.setFitView(polygon);
    //console.log(cmpData);
  }
  TemporaryGroup.prototype.onSelectConfirmEvent = function(){
    var self = this;
    $('.temporary-group-buttons > .temporary-group-confirm').off('click').on('click', function(){
      var path=[];

      for(var key in self.selectedMember){
        path.push(self.selectedMember[key].position)
      }
      confirmSite(self.map.map,path);


      //临时对讲位置显示
      self.createTg(function(rlt){

        self.ws.temporaryTalk(rlt.body);
      }, function(e){
        self.hideAddTGDialogInfo();
        self.showCreateTGDialogError(e);
      });
    });
  }

  // 确定 - 添加按钮
  TemporaryGroup.prototype.onAddConfirmEvent = function(){
    var self = this;
    $('.temporary-group-buttons > .temporary-group-confirm').off('click').on('click', function(){
      self.addTgMembers(function(rlt){
        self.ws.temporaryTalkAdd(self.ws.tasks.currentId, self.selectedMap.keySet().join(','));
      }, function(e){
        self.showCreateTGDialogError(e);

      });
    });
  }

  TemporaryGroup.prototype.closeSelectDialog = function(groups){
    
  }

  TemporaryGroup.prototype.onFixPTTKey = function(ws){
    $('#tg_search_input').on('focus', function(){
      ws.rtpWs.offPttKey();
    });
    $('#tg_search_input').on('blur', function(){
      ws.rtpWs.onPttKeyDown();
      ws.rtpWs.onPttKeyUp();
    });
  }

  TemporaryGroup.prototype.showCreateTGDialogInfo = function(){
    $('#tg_select_mask').css('display', 'block');
    $('#tg_select_alert').css('display', 'block');
    $('#tg_select_alert').html('正在发起临时对讲，请稍后！');
  }

  TemporaryGroup.prototype.hideCreateTGDialogInfo = function(){
    $('#tg_select_mask').css('display', 'none');
    $('#tg_select_alert').css('display', 'none');
  }

  TemporaryGroup.prototype.showAddTGDialogInfo = function(){
    $('#tg_select_mask').css('display', 'block');
    $('#tg_select_alert').css('display', 'block');
    $('#tg_select_alert').html('正在添加临时组成员，请稍后！');
  }

  TemporaryGroup.prototype.hideAddTGDialogInfo = function(){
    $('#tg_select_mask').css('display', 'none');
    $('#tg_select_alert').css('display', 'none');
  }

  TemporaryGroup.prototype.showCreateTGDialogError = function(msg){
    $('#tg_select_mask').css('display', 'block');
    $('#tg_select_alert_error').css('display', 'block');
    $('#tg_select_alert_error_info').html(msg);
    
  }
  TemporaryGroup.prototype.hideCreateTGDialogError = function(){
    $('#tg_select_mask').css('display', 'none');
    $('#tg_select_alert_error').css('display', 'none');
  }

  // selected
  TemporaryGroup.prototype.selectByGid = function(gid){

  }

  TemporaryGroup.prototype.unSelectByGid = function(gid){

  }

  TemporaryGroup.prototype.selectByUid = function(uid){

  }

  TemporaryGroup.prototype.unSelectByUid = function(uid){

  }
  TemporaryGroup.prototype.createTGDialog = function(){

  }
  // TemporaryGroup.prototype.clearSelected = function(){
  //   this.selectedMap.clear();
  //   $('input[name="tg_select_checkbox"]').attr("checked", false);
  // }

  // tg
  TemporaryGroup.prototype.initTGDialog = function(selectedMap){
    if(selectedMap){
      for(var i in selectedMap.map){
        this.addTGDialogLi(selectedMap.get(i));
        this.tgMemberMap.put(i, selectedMap.get(i));
      }
    }
    $('#tg_member_num').html(this.tgMemberMap.size());
    this.selectedMap.clear();
    $('.modal-backdrop').css('display','none');
  }
  TemporaryGroup.prototype.openTGDialog = function(){
    $('div[role="dialog"]').modal('hide');
    $('#temporaryGroup').modal('show');
  }

  TemporaryGroup.prototype.showTGDialog = function(){
    $('div[role="dialog"]').modal('hide');
    $('#temporaryGroup').modal('show');
    $('.modal-backdrop').css('display','none');
  }

  TemporaryGroup.prototype.onTGDialogEvent = function(){
    var self = this;
    $('.temporaryGroup-dialog-add-li').on('click', function(){
      $('.temporary-group-buttons > .btn').attr('disabled','disabled');
      self.openAddSelectDialog();
    });
    $('.temporaryGroup-dialog-dismiss-btn').on('click', function(){
      $('#confirmInfo').html('确定解散当前临时组?');
      $('#confirm_btn').off('click').on('click', function(){//临时对讲
        self.ws.temporaryTalkHangUp(self.ws.cid, self.ws.sid, self.ws.tasks.currentId,self.groupName);
        self.ws.overTalkSite(self.ws.slidebar.map.map);
      });
      $('#confirm_modal').modal('show');

    });
    $('#tg_select_alert_error_btn,#tg_select_alert_error_x').on('click', function(){
      self.hideCreateTGDialogError();
    });
  }

  var oneSecondTask = function(container, memberId){
    var self = container;
    self.timeCheckJoinMap.put(memberId, setTimeout(function(){
      if(self.joinedMembers[memberId] !== undefined){
        self.memberJoinTG(memberId);
        clearTimeout(container.timeCheckJoinMap.get(memberId));
        self.timeCheckJoinMap.remove(memberId);
        delete self.joinedMembers[memberId];
      }else{
        oneSecondTask(container, memberId);
      }
    }, 1000));
  }

  TemporaryGroup.prototype.addTGDialogLi = function(member){
    var self = this;
    if(member){
      $('.temporaryGroup-dialog-members-ul').append('<li id="tg_dialog_li_'
        + member.memberId + '" tg-dialog-val="' + member.memberId
        + '"><div class="waitting"><img id="tg_dialog_li_img_' + member.memberId
        + '" src="images/waitting.gif"></div><lable>'
        + member.memberName + '</lable></li>');
      $('input[value="' + member.memberId + '"]').attr('disable','disable');
      if(self.isCreated){
          self.timeTaskMap.put(member.memberId, setTimeout(function() {
            $('#tg_dialog_li_' + member.memberId).remove();
            customCheckboxChange('input[value="' + member.memberId + '"]', false);
            $('input[value="' + member.memberId + '"]').removeAttr('disabled');
            $('#tg_member_num').html(self.tgMemberMap.size());
            clearTimeout(self.timeCheckJoinMap.get(member.memberId));
            self.timeCheckJoinMap.remove(member.memberId);
            clearTimeout(self.timeTaskMap.get(member.memberId));
            self.timeTaskMap.remove(member.memberId);
            delete self.joinedMembers[member.memberId];
          }, 30000));
          oneSecondTask(self, member.memberId);
      }else{
        $('#tg_dialog_li_img_' + member.memberId).remove();
      }
    }
  }

  TemporaryGroup.prototype.memberJoinTG = function(memberId){
    var self = this;
    if(memberId){
      $('#tg_dialog_li_img_' + memberId).remove();
      clearTimeout(self.timeTaskMap.get(memberId));
      self.timeTaskMap.remove(memberId);
    }
  }

  TemporaryGroup.prototype.memberLevelTG = function(memberId){
    var self = this;
    var member = self.tgMemberMap.get(memberId);
    if(memberId && member !== undefined){
      self.tgMemberMap.remove(memberId);
      $('#tg_member_num').html(this.tgMemberMap.size());
      customCheckboxChange('input[value="' + memberId + '"]', false);
      $('input[value="' + memberId + '"]').removeAttr('disabled');
      $('#tg_dialog_li_' + memberId).remove();
      if(self.tgMemberMap.size() === 0){
        self.ws.temporaryTalkHangUp(self.ws.cid, self.ws.sid, self.ws.tasks.currentId,self.groupName);
        self.ws.overTalkSite(self.ws.slidebar.map.map);
      }else{
        self.showTGMemberLeave(member.memberName);
        setTimeout(function(){
          self.hideTGMemberLeave();
        },5000);
      }
    }
  }

  TemporaryGroup.prototype.clearTGDialog = function(){
    for(var i in this.tgMemberMap.map){
      this.clearTGDialogLi(i);
    }
  }

  TemporaryGroup.prototype.clearTGDialogLi = function(memberId){
    if(memberId){
      delete this.joinedMembers[memberId];
      this.tgMemberMap.remove(memberId);
      $('#tg_dialog_li_' + memberId).remove();
      $('input[value="' + memberId + '"]').removeAttr('disabled');
    }
  }
  //tg_leave_alert
  TemporaryGroup.prototype.showTGMemberLeave = function(msg){
    $('#tg_alert').html(msg + ' 离开临时对讲组');
    $('#tg_alert').css('display', 'block');
  }

  TemporaryGroup.prototype.hideTGMemberLeave = function(){
    $('#tg_alert').css('display', 'none');
  }

  TemporaryGroup.prototype.createTg = function(success, error){
    var self = this;
    var data = {
      'userid': parseInt(self.ws.uid),
      'userType': _const.DEVICE.DISPATCHER,
      'companyid': parseInt(self.ws.cmpid),
      'timestamp': fnUtils.getUnixTime() + '',
      'key': self.ws.key,
      'groupType': _const.PTT_TYPE.TEMPORARY_GROUP,   //临时组
      'memberIds': self.selectedMap.keySet().join(',')
    };
    self.showCreateTGDialogInfo();
    fnUtils.commonPost('group/createTemporaryGroup.do', data,
      function(rlt){
        console.log('9003', rlt);
        if(rlt.status === _const.HTTP.RES_STATUS.SUCCESS){
          if(rlt.error.code === _const.HTTP.RES_STATUS.ERROR_CODE.FAIL){
            if(error){
              error(rlt.error.message);
            }
          }else{
            setTimeout(function(){
              success(rlt);
            }, 2000);
          }
        }else{
          if(error){
            error(rlt.error.message);
          }
        }
      },
      function(e){
        console.log('group/createTemporaryGroup.do', e);
        error(e.statusText);
      }
    );
  }

  TemporaryGroup.prototype.addTgMembers = function(success, error){
    var self = this;
    var data = {
      'userid': parseInt(self.ws.uid),
      'userType': _const.DEVICE.DISPATCHER,
      'companyid': parseInt(self.ws.cmpid),
      'timestamp': fnUtils.getUnixTime() + '',
      'key': self.ws.key,
      'groupId': self.ws.tasks.currentId,
      'memberIds': self.selectedMap.keySet().join(',')
    };
    
    self.showAddTGDialogInfo();
    fnUtils.commonPost('group/joinTemporaryGroup.do', data,
      function(rlt){
        if(rlt.status === _const.HTTP.RES_STATUS.SUCCESS){
          if(rlt.error.code === _const.HTTP.RES_STATUS.ERROR_CODE.FAIL){
            if(error){
              error(rlt.error.message);
            }
          }else{
            setTimeout(function(){
              success(rlt);
            }, 2000);
          }
        }else{
          if(error){
            error(rlt.error.message);
          }
        }
      },
      function(e){
        error(e.statusText);
      }
    );
  }

  return TemporaryGroup;
});